{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "净现值（NPV）=未来现金净流量现值一原始投资额现值\n",
    "\n",
    "[净现值](https://baike.baidu.com/item/%E5%87%80%E7%8E%B0%E5%80%BC/500068)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "净现值 (NPV): 689.59\n"
     ]
    }
   ],
   "source": [
    "def calculate_npv(cash_flows, discount_rate):\n",
    "    \"\"\"\n",
    "    计算净现值（NPV）\n",
    "    \n",
    "    参数：\n",
    "    - cash_flows: 各期现金流列表（正现金流为收入，负现金流为支出）\n",
    "    - discount_rate: 贴现率（例如 5% 就传入 0.05）\n",
    "    \n",
    "    返回：\n",
    "    - NPV值\n",
    "    \"\"\"\n",
    "    npv = 0\n",
    "    for t, cash_flow in enumerate(cash_flows):\n",
    "        npv += cash_flow / (1 + discount_rate) ** t\n",
    "    return npv\n",
    "\n",
    "# 示例：假设初始投资为-1000，之后5年分别产生 200, 300, 400, 500, 600 的现金流，贴现率为 5%。\n",
    "cash_flows = [-1000, 200, 300, 400, 500, 600]\n",
    "discount_rate = 0.05 # 基准利率\n",
    "\n",
    "npv = calculate_npv(cash_flows, discount_rate)\n",
    "print(f\"净现值 (NPV): {npv:.2f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内部收益率 (IRR): 3.22%\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.optimize import fsolve\n",
    "\n",
    "def calculate_irr(cash_flows):\n",
    "    \"\"\"\n",
    "    计算内部收益率（IRR）\n",
    "    \n",
    "    参数：\n",
    "    - cash_flows: 各期现金流列表（正现金流为收入，负现金流为支出）\n",
    "    \n",
    "    返回：\n",
    "    - 内部收益率（IRR）\n",
    "    \"\"\"\n",
    "    # 定义净现值函数（NPV函数）\n",
    "    def npv_function(r):\n",
    "        npv = sum(cash_flow / (1 + r) ** t for t, cash_flow in enumerate(cash_flows))\n",
    "        return npv\n",
    "    \n",
    "    # 使用fsolve求解NPV为零时的贴现率r（即IRR）\n",
    "    irr = fsolve(npv_function, 0.1)  # 0.1为初始猜测（10%）\n",
    "    \n",
    "    return irr[0]  # 返回找到的IRR\n",
    "\n",
    "# 示例：假设初始投资为-1000，之后5年分别产生 200, 300, 400, 500, 600 的现金流\n",
    "# cash_flows = [-1000, 200, 300, 400, 500, 600]\n",
    "cash_flows = [-50000, -50000, -50000, 0, 165000]\n",
    "\n",
    "irr = calculate_irr(cash_flows)\n",
    "print(f\"内部收益率 (IRR): {irr * 100:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终本息和为: -165000.40 元\n"
     ]
    }
   ],
   "source": [
    "def calculate_savings_with_varying_deposits(deposits, annual_rate, years):\n",
    "    \"\"\"\n",
    "    计算每年存入不同金额的存款的总收益\n",
    "    \n",
    "    参数：\n",
    "    - deposits: 每年的存款金额列表（长度为 years）\n",
    "    - annual_rate: 年利率（例如 5% 为 0.05）\n",
    "    - years: 总存期（存款的年数）\n",
    "    \n",
    "    返回：\n",
    "    - 总本息和（最终余额）\n",
    "    \"\"\"\n",
    "    total_amount = 0\n",
    "    for t in range(years):\n",
    "        # 计算每年存款的最终本息\n",
    "        total_amount += deposits[t] * (1 + annual_rate) ** (years - t - 1)\n",
    "    \n",
    "    return total_amount\n",
    "\n",
    "# 示例：每年存款不同金额，年利率5%，存期10年\n",
    "# deposits = [1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500]  # 每年存入的金额\n",
    "deposits = [-50000, -50000, -50000, 0, 0]\n",
    "annual_rate = 0.032166  # 年利率 5%\n",
    "years = len(deposits)  # 存期 10年\n",
    "\n",
    "total_balance = calculate_savings_with_varying_deposits(deposits, annual_rate, years)\n",
    "print(f\"最终本息和为: {total_balance:.2f} 元\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
